#include "head.h"
// 初始化会话进程池
int initConversationPool(conversation_status_t * ConversationPool,int i)
{
    for(int i = 0;i < 4;i++)
    {
        // 使用socketpair创建本地父进程与子进程通信的方式
        int SonFd[2];
        socketpair(AF_LOCAL,SOCK_STREAM,0,SonFd);
        
        int pid = fork();

        if(pid == 0)
        {
            close(SonFd[1]);
            printf("son++ %d\n",i);
            // 开启子进程
            starSon(SonFd[0]);
            
            printf("son++ 成功 %d\n",i);
        }
        else
        {
            ConversationPool[i].pid = pid;
            ConversationPool[i].status = FREE;
            ConversationPool[i].socket_local_fd = SonFd[1]; 

            close(SonFd[0]);
        }
    }
    return 0;
}

// 将新的文件描述符所指指针拷贝给子进程池
int toConversationPool(int NetFd,conversation_status_t *ConversationPool,int i)
{
    for(int j = 0;j < i;j++)
    {
        if(ConversationPool[j].status == FREE)
        {
            printf("开始copy！%d\n",ConversationPool[j].socket_local_fd);
            copyNetFd(&NetFd,ConversationPool[j].socket_local_fd);
            printf("来活了！%d\n",ConversationPool[j].socket_local_fd);
            
            ConversationPool[j].status = BUSY;
            break;
        }
    }
    return 0;
}

